var e = require, t = module, n = exports;

Object.defineProperty(n, "__esModule", {
    value: !0
});

var o = e("Global"), i = e("AuthManager"), a = e("PetManager"), r = e("GameConfig"), s = e("GameData"), c = e("EggsManager"), l = e("ActivityManager"), d = e("WebManager"), u = function() {
    function u() {}
    return u.getInstance = function() {
        return this._manager || (this._manager = new u()), this._manager;
    }, u.prototype.getJson = function() {
        return s.GD.isNew = 0, s.GD.saveTime = Date.now(), s.GD.eggDatas = 0 < c.default.getInstance().eggDataMap.size ? Array.from(c.default.getInstance().eggDataMap.values()) : [], 
        s.GD.eggPDatas = 0 < c.default.getInstance().eggPDataMap.size ? Array.from(c.default.getInstance().eggPDataMap.values()) : [], 
        s.GD.eggCDatas = 0 < c.default.getInstance().eggCDataMap.size ? Array.from(c.default.getInstance().eggCDataMap.values()) : [], 
        (null == s.GD.money || s.GD.money < 0) && (s.GD.money = 0), (null == s.GD.diamond || s.GD.diamond < 0) && (s.GD.diamond = 0), 
        (null == s.GD.piece || s.GD.piece < 0) && (s.GD.piece = 0), s.GD;
    }, u.prototype.upload = function(e) {
        var a;
        void 0 === e && (e = {
            json: !0,
            local: !0
        }), GA.Info.underCheck && r.GC.isDebug || !o.G.isLoad || (o.G.platform == o.Platform.H4399 ? e.json && (a = JSON.stringify(this.getJson()), 
        cc.sys.localStorage.setItem("data", a), console.log("本地数据存储成功"), e.local ? this.save({
            data: a,
            interval: 0
        }) : s.GD.saveLastTime = Date.now()) : GA.Info.uid && ((a = {}).userId = GA.Info.uid, 
        e.json && (a.data = JSON.stringify(this.getJson()), e.local ? this.save({
            data: a.data,
            interval: 0
        }) : s.GD.saveLastTime = Date.now()), e.guanqia && (a.guanqia = e.guanqia), e.skin && (a.skin = e.skin), 
        e.level && (a.level = e.level), e.killNum && (a.killNum = e.killNum), e.compose && (a.compose = e.compose), 
        d.default.getInstance().reqSaveUser({
            data: a,
            success: function() {}
        })));
    }, u.prototype.save = function(e) {
        if (void 0 === e && (e = {
            data: JSON.stringify(this.getJson()),
            interval: 0
        }), (!GA.Info.underCheck || !r.GC.isDebug) && o.G.isLoad) {
            cc.log("localSave");
            var a = Date.now();
            if (a - s.GD.saveLastTime > e.interval || s.GD.saveLastTime > a) {
                s.GD.saveLastTime = a, e.data || (e.data = JSON.stringify(this.getJson()));
                try {
                    GA.isWeChat() || GA.isQQ() ? GA.PA.setStorage({
                        key: "data",
                        data: e.data,
                        success: function() {},
                        fail: function() {}
                    }) : cc.sys.localStorage.setItem("data", e.data);
                } catch (e) {}
            }
        }
    }, u.prototype.sync = function(e) {
        GA.Info.underCheck && r.GC.isDebug || (o.G.platform == o.Platform.H4399 ? (cc.sys.localStorage.setItem("data", e), 
        console.log("本地数据存储成功")) : GA.Info.uid && d.default.getInstance().reqSaveUser({
            data: {
                userId: GA.Info.uid,
                data: e,
                guanqia: s.GD.level
            },
            success: function() {}
        }));
    }, u.prototype.reqRank = function(n) {
        d.default.getInstance().reqRank({
            success: function(e) {
                if (0 == e.data.code && e.data.data.rankList instanceof Array) {
                    var a = e.data.data.rankList.map(function(e) {
                        return {
                            openid: e.openid,
                            level: e.level,
                            killNum: e.killNum,
                            avatarUrl: e.headpic,
                            nickname: e.nickname
                        };
                    }).sort(function(e, a) {
                        return isNaN(e.level) || isNaN(e.killNum) ? 1 : isNaN(a.level) || isNaN(a.killNum) ? -1 : e.level === a.level ? a.level + a.killNum - (e.level + e.killNum) : a.level - e.level;
                    }), t = {
                        openid: e.data.data.userRank.openid,
                        level: e.data.data.userRank.level,
                        killNum: e.data.data.userRank.killNum,
                        avatarUrl: s.GD.playerData.icon,
                        nickname: s.GD.playerData.nickname
                    };
                    try {
                        GA.PA && GA.PA.getOpenDataContext && GA.PA.getOpenDataContext().postMessage({
                            messageType: 5,
                            MAIN_MENU_NUM: n,
                            score: s.GD.level,
                            count: s.GD.killNum,
                            rankList: JSON.stringify(a),
                            userData: JSON.stringify(t),
                            rank: e.data.data.userRank.rank
                        });
                    } catch (e) {}
                }
            }
        }), d.default.getInstance().saveRank({
            data: {
                userid: GA.Info.uid,
                level: s.GD.level
            }
        });
    }, u.prototype.getData = function(n) {
        return new Promise(function(t) {
            try {
                var e, a, s = Date.now();
                !isNaN(n.saveLastTime) && n.saveLastTime > s && (n.saveLastTime = s), GA.isPlatform([ GA.Platform.WeChat, GA.Platform.QQ, GA.Platform.ByteDance ]) ? GA.PA.getStorage({
                    key: "data",
                    success: function(e) {
                        try {
                            var a = e.data ? JSON.parse(e.data) : {};
                            !isNaN(a.saveLastTime) && a.saveLastTime > s && (a.saveLastTime = s), isNaN(a.saveLastTime) || isNaN(n.saveLastTime) ? isNaN(a.saveLastTime) ? t(n) : (u.getInstance().sync(e.data), 
                            t(a)) : a.saveLastTime > n.saveLastTime ? (u.getInstance().sync(e.data), t(a)) : t(n);
                        } catch (e) {
                            t(n);
                        }
                    },
                    fail: function() {
                        t(n);
                    }
                }) : (a = (e = cc.sys.localStorage.getItem("data")) ? JSON.parse(e) : {}, !isNaN(a.saveLastTime) && a.saveLastTime > s && (a.saveLastTime = s), 
                isNaN(a.saveLastTime) || isNaN(n.saveLastTime) ? isNaN(a.saveLastTime) ? t(n) : (u.getInstance().sync(e), 
                t(a)) : a.saveLastTime > n.saveLastTime ? (u.getInstance().sync(e), t(a)) : t(n));
            } catch (e) {
                console.error("调用读取本地数据接口失败", e), t(n);
            }
        });
    }, u.prototype.loadConfig = function(e) {
        r.GC.load(e), GA.Info.channelId && (o.G.isMpCheckChannel = r.GC.MpCheckChannel.split("+").some(function(e) {
            return e == GA.Info.channelId;
        })), r.GC.eggConfig.forEach(function(e) {
            c.default.getInstance().eggConfigMap.set(e.idx, e);
        }), r.GC.eggPConfig.forEach(function(e) {
            c.default.getInstance().eggPConfigMap.set(e.idx, e);
        }), r.GC.eggCConfig.forEach(function(e) {
            c.default.getInstance().eggCConfigMap.set(e.idx, e);
        }), r.GC.activityConfig.forEach(function(e) {
            l.ActivityManager.getInstance().activityConfigMap.set(e.idx, e);
        }), r.GC.bannerConfigList.forEach(function(e) {
            o.G.bannerConfigMap.set(e.tag, e), o.G.bannerDataMap.set(e.tag, {
                tag: e.tag,
                count: 0
            });
        }), o.G.bannerConfigMap.set(r.GC.bannerTag, {
            tag: r.GC.bannerTag,
            count: r.GC.bannerCount
        }), o.G.bannerDataMap.set(r.GC.bannerTag, {
            tag: r.GC.bannerTag,
            count: 0
        }), r.GC.guideConfigList.forEach(function(e) {
            o.G.guideConfigMap.set(e.tag, e), o.G.guideDataMap.set(e.tag, {
                tag: e.tag
            });
        });
    }, u.prototype.loadData = function(e) {
        try {
            s.GD.load(e);
            var n = Date.now();
            s.GD.skins.indexOf(s.GD.personIdx) < 0 && (s.GD.personIdx = s.GD.lastPersonIdx), 
            s.GD.moneyLastTime = n, 1 == r.GC.isFreeTry ? s.GD.tryFreeCount = s.GD.isNew ? this.getNumber(parseInt(o.G.Config.get("freeTryCount")), s.GD.tryFreeCount) : this.getNumber(e.tryFreeCount, s.GD.tryFreeCount) : s.GD.tryFreeCount = 0;
            var t = new Date().setHours(0, 0, 0, 0);
            s.GD.refreshTime < t ? (s.GD.refreshTime = t, s.GD.tryFailCount = 0, s.GD.emptyFailCount = 0, 
            s.GD.shareCount = 0, s.GD.diamondCount = 0, s.GD.pieceCount = 0, s.GD.recorderCount = 0, 
            s.GD.isNotice = 0, s.GD.isNewYearPop = 1, s.GD.isNewActivityPop = 1, s.GD.isChristmasPop = 1, 
            s.GD.isChristmasReward = 0, s.GD.christmasPlayCount = 0, s.GD.christmasDrawCount = 0, 
            s.GD.olympicDrawTime = 0, s.GD.videoAdDataList = [], i.default.getInstance().reset(), 
            s.GD.eggDatas.forEach(function(e) {
                e.count = 0, c.default.getInstance().eggDataMap.set(e.idx, e);
            }), s.GD.eggPDatas.forEach(function(e) {
                e.count = 0, c.default.getInstance().eggPDataMap.set(e.idx, e);
            }), s.GD.eggCDatas.forEach(function(e) {
                e.count = 0, c.default.getInstance().eggCDataMap.set(e.idx, e);
            })) : (s.GD.eggDatas.forEach(function(e) {
                c.default.getInstance().eggDataMap.set(e.idx, e);
            }), s.GD.eggPDatas.forEach(function(e) {
                c.default.getInstance().eggPDataMap.set(e.idx, e);
            }), s.GD.eggCDatas.forEach(function(e) {
                c.default.getInstance().eggCDataMap.set(e.idx, e);
            }), s.GD.bannerDatas.forEach(function(e) {
                o.G.bannerDataMap.set(e.tag, e);
            }), s.GD.guideDatas.forEach(function(e) {
                o.G.guideDataMap.set(e.tag, e);
            })), (null == s.GD.money || s.GD.money < 0) && (s.GD.money = 0), (null == s.GD.diamond || s.GD.diamond < 0) && (s.GD.diamond = 0), 
            (null == s.GD.piece || s.GD.piece < 0) && (s.GD.piece = 0), 0 < s.GD.eggLastTime && -1 == c.default.getInstance().checkEggGun(0) && c.default.getInstance().updateEggGun(0), 
            s.GD.isEggs && -1 == c.default.getInstance().checkEggPet(0) && c.default.getInstance().updateEggPet(0), 
            s.GD.seasonOpenTime < s.GD.seasonLastTime && (s.GD.isOpen = 1), s.GD.seasonRewardTime < s.GD.seasonLastTime && (s.GD.isReward = 1), 
            s.GD.petDataIdx = a.default.getInstance().checkPetDataIdx(s.GD.petDataIdx), s.GD.petDataList.forEach(function(e, t) {
                e && n - e.time > r.GC.petChargeTime && (e.hp = a.default.getInstance().getHp(t));
            });
            var u = a.default.getInstance().getRealData(0);
            !u.has && s.GD.isPet && (u.has = 1), u.form < s.GD.petIdx && (u.form = s.GD.petIdx), 
            u.lv < s.GD.petLevel && (u.lv = s.GD.petLevel), u.time < s.GD.emptyLastTime && (u.time = s.GD.emptyLastTime), 
            u.hp < s.GD.pet_hp && (u.hp = s.GD.pet_hp);
        } catch (e) {
            console.error(e);
        }
        o.G.isLoad = !0;
    }, u.prototype.loadRes = function(s) {
        cc.resources.load([ "config/gun", "config/person", "config/pet", "config/eggGun", "config/machine", "config/machineGun", "config/eggPerson", "config/drawGun", "config/olympicGun" ], cc.JsonAsset, function(e, a) {
            if (e) return console.error(e);
            try {
                var t;
                o.G.jsons = a.map(function(e) {
                    return e.json;
                }), o.G.jsons[0].forEach(function(e) {
                    return o.G.gunModelList.push(e);
                }), o.G.jsons[1].forEach(function(e) {
                    return o.G.personModelList.push(e);
                }), r.GC.seasonAuto ? (0 < (t = o.G.checkSeason()) && (o.G.season = t), o.G.seasonGunIdx = r.GC.seasonGunList[(o.G.season - 1) % r.GC.seasonGunList.length], 
                o.G.seasonPersonIdx = r.GC.seasonPersonList[(o.G.season - 1) % r.GC.seasonPersonList.length]) : (o.G.seasonGunIdx = r.GC.maxGunIdx, 
                o.G.seasonPersonIdx = r.GC.maxPersonIdx), o.G.jsons[3].forEach(function(e) {
                    return o.G.eggGunModelList.push(e);
                }), o.G.jsons[4].forEach(function(e) {
                    return o.G.machineModelList.push(e);
                }), o.G.jsons[5].forEach(function(e) {
                    return o.G.machineGunModelList.push(e);
                }), o.G.jsons[6].forEach(function(e) {
                    return o.G.eggPersonModelList.push(e);
                }), o.G.jsons[7].forEach(function(e) {
                    return o.G.drawGunModelList.push(e);
                }), o.G.jsons[8].forEach(function(e) {
                    return o.G.olympicGunModelList.push(e);
                });
            } catch (e) {
                console.error(e);
            }
            s();
        });
    }, u.prototype.getNumber = function(e, a) {
        return isNaN(e) ? a : e;
    }, u.prototype.getObject = function(e, a) {
        return e || a;
    }, u.prototype.getInfo = function(e, a) {
        if (!e) return a;
        try {
            return JSON.parse(e);
        } catch (e) {
            return a;
        }
    }, u._manager = null, u;
}();

n.default = u;